package com.showdor.springboot;

import org.apache.commons.lang3.time.DateUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

/**
 * @author showdor
 * @email chuanqi@outlook.com
 * @date 2023/10/30
 */
public class Insert2Sqlserver {

    public static void main(String[] args) {
        try {
            System.out.println(Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"));
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
        int num = 10_000_000;
        //2022-10-01 10:57:09
        Date time = new Date();
        try (Connection connection = new StudioSqlServerConnection().getConnection()) {
            String sql = "INSERT INTO vehicle_pass (MotorVehicleID,PlateClass,PlateNo,VehicleClass,VehicleBrand,create_time,Pass_Time) values (?,?,?,?,?,?,?)";
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            for (int i = 1; i <= num; i++) {
                time = DateUtils.addSeconds(time, 1);
                preparedStatement.setString(1, "ID_" + time.getTime());
                preparedStatement.setString(2, "1");
                preparedStatement.setString(3, randomPlateNO());
                preparedStatement.setString(4, "1");
                preparedStatement.setString(5, "1");
                preparedStatement.setString(6, com.alibaba.fastjson2.util.DateUtils.format(time, "yyyy-MM-dd HH:mm:ss"));
                preparedStatement.setString(7, com.alibaba.fastjson2.util.DateUtils.format(time, "yyyyMMddHHmmss"));
                preparedStatement.addBatch();
                if (i % 1000 == 0) {
                    preparedStatement.executeBatch();
                    System.out.println("批量插入成功"+i);
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public static String randomPlateNO() {
        String plateNO = "";
        //省号
        String[] sheng = {"京", "津", "沪", "渝", "冀", "吉", "辽", "黑", "湘", "鄂", "甘", "晋", "陕", "豫", "川", "云", "桂",
                "蒙", "贵", "青", "藏", "新", "宁", "粤", "琼", "闽", "苏", "浙", "赣", "鲁", "皖"};
        int i = (int) (Math.random() * (sheng.length));
        plateNO += sheng[i];
        //字母
        plateNO += getA();
        //字母总数
        int Anum = (int) (Math.random() * 3);
        Set<Integer> set = new HashSet<>();
        while (set.size() != Anum) {
            set.add((int) (Math.random() * 5));
        }
        //插入编号
        for (int k = 0; k < 5; k++) {
            if (set.contains(k)) {
                plateNO += getA();
            } else {
                plateNO += (int) (Math.random() * 10);
            }
        }
        return plateNO;
    }

    //获取一个随机字母
    public static String getA() {
        int j = 0;
        do {
            j = Integer.valueOf('A') + (int) (Math.random() * 26);
        }
        while ((j == 73) || (j == 79));
        return "" + (char) j;
    }
}
